/********************************************
 * 功能说明: 
 * 模块名称: 
 * 系统名称: 
 * 软件版权: Frank
 * 系统版本: 1.0.0
 * 开发人员: Frank
 * 开发时间: 2019/11/8 15:48
 * 审核人员: 
 * 相关文档: 
 * 修改记录: 修改日期 修改人员 修改说明
 *********************************************/
package com.spring.arch.uaa.web.converter;

import com.spring.arch.uaa.web.dto.WebUserForListDTO;
import com.spring.arch.uaa.model.OrganizationEmployee;
import com.spring.arch.uaa.model.User;
import com.spring.arch.uaa.repository.OrganizationEmployeeRepository;
import com.spring.arch.uaa.web.dto.WebUserForListDTO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户转换器
 *
 * @author Frank
 * @version 1.0.0.1
 * @since 2019/11/8 15:48
 */
@Component
public class WebUserForListConverter {

    @Autowired
    private OrganizationEmployeeRepository organizationEmployeeRepository;

    public WebUserForListDTO toDTO(@NonNull final User model) {
        final WebUserForListDTO dto = new WebUserForListDTO();
        dto.setId(model.getId());
        dto.setAccount(model.getAccount());
        dto.setMobile(model.getMobile());
        dto.setRealName(model.getRealName());

        List<OrganizationEmployee> list = organizationEmployeeRepository.findByUserAndStatusTrue(model);

        if(CollectionUtils.isNotEmpty(list)) {

            dto.setEmployeeNo(list.get(0).getEmployee().getCode());
        }
        return dto;
    }

    public List<WebUserForListDTO> toListDTO(final Collection<User> models) {
        if(CollectionUtils.isEmpty(models)) {
            return Collections.emptyList();
        }
        return models.stream().map(this :: toDTO).collect(Collectors.toList());
    }
}
